<?php


namespace app\common\model;


use app\admin\model\product\Log as ProductLog;
use think\Db;
use think\Model;

class ProductBuy extends Model
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [

    ];

    /***
     * 结算收益
     * @param $value
     * @param $data
     * @return string
     */
    public static function buyBalance()
    {
        // 启动事务
        Db::startTrans();
        try{
            //获取所有用户购买产品进行中;
            $data = self::where(['status' => 1])->select();

            $desc1  = 0;//过期 数量
            $desc2  = 0;//未到收益时间 数量
            $desc3  = 0;//发放 数量

            foreach ($data as $k => $v) {
                $product = Product::get(['id' => $v->product_id, 'status' => 1]);
                //如果当前产品没有在进行中
                if(!$product) {
                    //更新当前购买产品状态
                    $model = self::get($v->id);
                    $status = ($product->status == 2 || $product->status == 3) ? 2 : 0;
                    $model->save(['status' => $status]);

                    $desc1++;
                    continue;
                }

                //产品开始时间和结束时间
                $productTime = explode("-", $product['workrange']);
                if($productTime[0] > time() || $productTime[1] < time()){
                    continue;
                }

                //根据购买时间进行每天24小时收益
                $time = time();//当前时间
                $storetime = 24*60*60;//一天时间戳
                //未到收益时间点
                if(($time - $v->nexttime) < $storetime) {
                    $desc2++;
                    continue;
                }

                $memo = $product->title.'投资收益'; //说明

                $money = $v->price * ($v->ratio / 100);

                //更新时间
                $buy = self::get($v->id,$money,$memo);
                $buy->save(['nexttime' => ($v->nexttime + $storetime)]);

                ProductLog::interest($v->id);//新增返利记录

                User::money($money,$v->user_id,$memo);//更新用户余额

                User::setTeamMoney($v->user_id,$money,"团队佣金"); //设置团队佣金

                $desc3++;

            }
            echo "过期数量".$desc1 ." "."未到收益时间数量".$desc2." "."发放数量".$desc3;
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
    }

    /***
     * 周期结束
     * @param $value
     * @param $data
     * @return string
     */
    public static function weekOver()
    {
        // 启动事务
        Db::startTrans();
        try{
            //获取所有用户购买产品进行中;
            $data = self::where(['status' => 1])->select();
            foreach ($data as $k => $v) {
                $product = Product::get(['id' => $v->product_id, 'status' => 1]);

                //产品开始时间和结束时间
                $productTime = explode("-", $product['workrange']);

                //周期结束
                if($productTime[1] <= time()){
                    $product->save(['status' => 3]);//更新结束

                    //更新当前购买产品状态
                    $model = self::get($v->id);
                    $model->save(['status' => 3]);

                    if($product->way == 2) {//等额本金
                        User::money($model->price,$v->user_id,'返回本金');//更新用户余额
                    }

                    continue;
                }
            }

            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
    }

}